using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using FitnessMVC.Data;
using FitnessMVCWeb.Helpers;
using System.IO;

namespace FitnessMVCWeb.Controllers
{
    public class UserPictureController : Controller
    {

        IFitnessRepository fitnessRepository;

        // Pattern from NerdDinner
        // (Poor Mans) Dependency Injection enabled constructors

        public UserPictureController()
            : this(new SqlFitnessRepository()) {
        }

        public UserPictureController(IFitnessRepository repository)
        {
            fitnessRepository = repository;
        }

        //
        // GET: /UserPicture/

        [Authorize]
        public ActionResult Index()
        {
            if (User == null)
                return View("LogOn");

            var pics = fitnessRepository.GetUserPictures(User.Identity.Name);
            if (pics == null)
                return View(new List<UserPicture>());

            return View(pics.ToList());
        }

        [Authorize]
        public ActionResult File(int id)
        {
            if (User == null)
                return View("LogOn");

            var pic = fitnessRepository.GetUserPicture(id);
            if (pic == null)
                return View();

            return PictureSaver.GetFileResult(pic);
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Create()
        {
            return View();
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateAntiForgeryToken]
        public ActionResult Create(bool isProfilePic)
        {
            if (User == null)
                return View("LogOn");

            if (Request.Files == null)
                return RedirectToAction("Index", "UserProfile", new { ErrMsg = "Must choose a picture to upload." });

            if (Request.Files.Count < 1)
                return RedirectToAction("Index", "UserProfile", new { ErrMsg = "No files were selected." });


            UserPicture pic = new UserPicture();            
            pic.UserId = fitnessRepository.GetUserByName(User.Identity.Name).UserId;
            
            // Save the file to the Disk.
            HttpPostedFileBase hpf = Request.Files[0] as HttpPostedFileBase;
            if (hpf.ContentLength == 0)
                return RedirectToAction("Index", "UserProfile", new { ErrMsg = "File was not valid." });
            
            string fileName = "";
            if (!PictureSaver.SaveFile(hpf, out fileName))
                return RedirectToAction("Index", "UserProfile", new { ErrMsg = "There was a problem uploading your file, make sure the file meets the size limit requirements." });

            pic.PicturePath = fileName;
            fitnessRepository.AddUserPicture(pic);
            fitnessRepository.Save();

            if (isProfilePic)
            {
                fitnessRepository.SetUserProfilePic(User.Identity.Name, pic);
                fitnessRepository.Save();
            }

            return RedirectToAction("Index", "UserPicture");
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(UserPicture pic)
        {
            if (User == null)
                return View("LogOn");

            fitnessRepository.DeleteUserPicture(pic);
            fitnessRepository.Save();

            return View("Index");
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SetProfilePic(int picId)
        {
            if (User == null)
                return View("LogOn");

            var usr = fitnessRepository.GetUserByName(User.Identity.Name);            
            var pic = fitnessRepository.GetUserPicture(picId);
            
            if (pic.UserId != usr.UserId)
                return RedirectToAction("Index", "UserProfile", new { ErrMsg = "Cannot set another users pic to your profile... maybe later... but not now..." });
            
            var profile = fitnessRepository.GetUserProfile(User.Identity.Name);
            if (profile.ProfilePicID != pic.PictureId)
            {
                profile.ProfilePicID = pic.PictureId;
                fitnessRepository.Save();
            }

            return RedirectToAction("Index", "UserProfile", new { InfoMsg = "Profile Picture Changed..." });
        }

    }
}
